#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <vector>



class Task
{
    private:
    int _x;
    int _y;

    public:
    Task(int x,int y):_x(x),_y(y)
    {

    }
    int Excute()
    {
        return _x+_y;
    }
    ~Task()
    {

    }
};


struct ThreadNode
{
    Task _t;
    std::string _name;
    ThreadNode(Task t,std::string name)
    :_t(t),_name(name)
    {

    }
};

void *rountine(void *argc)
{
    ThreadNode* t = static_cast<ThreadNode *>(argc);
    int cnt = 3;
    while (cnt--)
    {
        std::cout << "我是线程" << t->_name << "正在运行任务结果"<<t->_t.Excute() << std::endl;
        sleep(2);
    }

    pthread_exit((void *)23);
}

int main()
{
    std::vector<pthread_t> vt;
    for (int i = 0; i < 10; i++)
    {
        
        pthread_t t;
        char arr[64];
        sprintf(arr, "thread-%d", i);
        ThreadNode node({i,i+1},arr);
        int n = pthread_create(&t, nullptr, rountine, (void *)&node);
        if (n == 0)
        {
            vt.push_back(t);
        }
        else
        {
            continue;
        }
    }

    int cnt = 5;
    while (cnt--)
    {
        std::cout << "我是主线程，正在运行" << std::endl;
        sleep(1);
    }
    void *ret = nullptr;

    for (auto x : vt)
    {
        pthread_join(x, &ret);
        std::cout << "子线程退出码为：" << (long long)ret << std::endl;
    }

    return 0;
}